import { loadEnv, defineConfig, PluginOption } from 'vite';
import { createVuePlugin } from 'vite-plugin-vue2';
import { createSvgPlugin } from 'vite-plugin-vue2-svg';
import { createHtmlPlugin } from 'vite-plugin-html';
import obfuscator from 'rollup-plugin-obfuscator';
import progress from 'vite-plugin-progress';
// import { visualizer } from 'rollup-plugin-visualizer';
// import externalGlobals from 'rollup-plugin-external-globals';

import path from 'path';

const CWD = process.cwd();

export default ({ mode }) => {
  const { VITE_BASE_URL, VITE_COMPANY_NAME, VITE_SITE_NAME, VITE_SITE_TAG } = loadEnv(mode, CWD);
  return {
    base: VITE_BASE_URL,
    resolve: {
      alias: {
        '~': path.resolve(__dirname, './'),
        '@': path.resolve(__dirname, './src'),
      },
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              },
            },
          },
        ],
      },
      preprocessorOptions: {
        less: {
          modifyVars: {
            // 如需自定义组件其他 token, 在此处配置
          },
        },
      },
    },

    plugins: [
      createVuePlugin({
        jsx: true,
      }),
      createSvgPlugin(),
      createHtmlPlugin({
        minify: true,
        inject: {
          data: {
            company_name: VITE_COMPANY_NAME,
            site_name: VITE_SITE_NAME,
            site_tag: VITE_SITE_TAG,
          },
        },
      }),
      obfuscator({
        global: true,
        include: ['src/api/**', 'src/utils/**', 'src/permission.js', 'src/router/**'],
        options: {
          // Your javascript-obfuscator options here
          // See what's allowed: https://github.com/javascript-obfuscator/javascript-obfuscator
        },
      }),
      progress(),
      // visualizer(),
    ],

    build: {
      chunkSizeWarningLimit: 3000,
      cssCodeSplit: false,
      rollupOptions: {
        output: {
          entryFileNames: `assets/[hash].js`,
          chunkFileNames: `assets/[hash].js`,
          assetFileNames: `assets/[hash].[ext]`,
          manualChunks (id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          },
        },

        // 告诉打包工具 在external配置的 都是外部依赖项  不需要打包
        // external: ['tinymce', 'xlsx', 'ol-map-tool', 'vue-pdf-embed/dist/vue2-pdf-embed', 'echarts', 'tdesign-vue'],
        // plugins: [
        //   externalGlobals({
        //     // "在项目中引入的变量名称"："CDN包导出的名称，一般在CDN包中都是可见的"
        //     tinymce: 'tinymce',
        //     xlsx: 'XLSX',
        //     echarts: 'echarts',
        //     'tdesign-vue': 'TDesign',
        //     'ol-map-tool': 'OlMapTool',
        //     'vue-pdf-embed/dist/vue2-pdf-embed': 'VuePdfEmbed',
        //   }),
        // ],
      },
      // sourcemap: true,
    },
    define: {
      'process.env': {},
    },
    server: {
      host: '0.0.0.0',
      port: 3002,
      proxy: {
        '/prod-api': {
          // 用于开发环境下的转发请求
          // target: 'http://59.53.159.59:18081/prod-api', // 外网测试
          // target: 'http://111.75.227.156:20100/prod-api', // 外网交通厅正式
          // target: 'http://10.1.100.8/prod-api', // 内网测试
          // target: 'http://123.60.66.67/prod-api', // 外网交投集团正式
          // target: 'http://192.168.11.131:80/prod-api',// 何工电脑
          // target: 'http://192.168.11.178:80/prod-api', // 章工电脑
          target: 'http://10.0.0.83/prod-api', // 内网交投集团正式
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/prod-api/, ''),
        },
      },
    },
  };
};
